#include <stdio.h>
#include "menu.h"

// 定义菜单数量
#define MOTHER_MENU_COUNT 5
#define MAX_ROOT_MENU_COUNT 14
#define MAX_SUB_MENU_COUNT 5

// 菜单内容
const char *motherMenus[MOTHER_MENU_COUNT] = {
    "母菜单 1",
    "母菜单 2",
    "母菜单 3",
    "母菜单 4",
    "母菜单 5"
};

// 每个主菜单的根目录数量
const int rootMenuCounts[MOTHER_MENU_COUNT] = {14, 9, 2, 2, 2};

// 每个主菜单的根目录内容
const char *rootMenus[MOTHER_MENU_COUNT][MAX_ROOT_MENU_COUNT] = {
    {"根目录 1", "根目录 2", "根目录 3", "根目录 4", "根目录 5", "根目录 6", "根目录 7", "根目录 8", "根目录 9", "根目录 10", "根目录 11", "根目录 12", "根目录 13", "根目录 14"},
    {"根目录 1", "根目录 2", "根目录 3", "根目录 4", "根目录 5", "根目录 6", "根目录 7", "根目录 8", "根目录 9"},
    {"根目录 1", "根目录 2"},
    {"根目录 1", "根目录 2"},
    {"根目录 1", "根目录 2"}
};

// 第三个主菜单的子目录内容
const char *subMenus[MAX_ROOT_MENU_COUNT][MAX_SUB_MENU_COUNT] = {
    {"子目录 1", "子目录 2", "子目录 3", "子目录 4", "子目录 5"},
    {"子目录 1", "子目录 2", "子目录 3", "子目录 4", "子目录 5"}
};

// 当前菜单状态
typedef enum
{
    MENU_STATE_MOTHER,
    MENU_STATE_ROOT,
    MENU_STATE_SUB
} MenuState;

typedef struct
{
    MenuState state;       // 当前菜单状态
    int currentMotherMenu; // 当前母菜单索引
    int currentRootMenu;   // 当前根菜单索引
    int currentSubMenu;    // 当前子菜单索引
} MenuContext;

MenuContext menuContext = {MENU_STATE_MOTHER, 0, 0, 0};

// 显示当前菜单
void displayMenu(void)
{
    if (menuContext.state == MENU_STATE_MOTHER)
    {
        printf("当前母菜单: %s\n", motherMenus[menuContext.currentMotherMenu]);
    }
    else if (menuContext.state == MENU_STATE_ROOT)
    {
        printf("当前根目录: %s\n", rootMenus[menuContext.currentMotherMenu][menuContext.currentRootMenu]);
    }
    else if (menuContext.state == MENU_STATE_SUB)
    {
        printf("当前子目录: %s\n", subMenus[menuContext.currentRootMenu][menuContext.currentSubMenu]);
    }
}

// 切换到下一个菜单
void nextMenu(void)
{
    if (menuContext.state == MENU_STATE_MOTHER)
    {
        menuContext.currentMotherMenu = (menuContext.currentMotherMenu + 1) % MOTHER_MENU_COUNT;
    }
    else if (menuContext.state == MENU_STATE_ROOT)
    {
        int rootCount = rootMenuCounts[menuContext.currentMotherMenu];
        menuContext.currentRootMenu = (menuContext.currentRootMenu + 1) % rootCount;
    }
    else if (menuContext.state == MENU_STATE_SUB)
    {
        menuContext.currentSubMenu = (menuContext.currentSubMenu + 1) % MAX_SUB_MENU_COUNT;
    }
    displayMenu();
}

// 切换到上一个菜单
void previousMenu(void)
{
    if (menuContext.state == MENU_STATE_MOTHER)
    {
        menuContext.currentMotherMenu = (menuContext.currentMotherMenu - 1 + MOTHER_MENU_COUNT) % MOTHER_MENU_COUNT;
    }
    else if (menuContext.state == MENU_STATE_ROOT)
    {
        int rootCount = rootMenuCounts[menuContext.currentMotherMenu];
        menuContext.currentRootMenu = (menuContext.currentRootMenu - 1 + rootCount) % rootCount;
    }
    else if (menuContext.state == MENU_STATE_SUB)
    {
        menuContext.currentSubMenu = (menuContext.currentSubMenu - 1 + MAX_SUB_MENU_COUNT) % MAX_SUB_MENU_COUNT;
    }
    displayMenu();
}

// 进入子菜单
void enterMenu(void)
{
    if (menuContext.state == MENU_STATE_MOTHER)
    {
        menuContext.state = MENU_STATE_ROOT;
        menuContext.currentRootMenu = 0; // 进入根菜单时从第一个开始
    }
    else if (menuContext.state == MENU_STATE_ROOT)
    {
        if (menuContext.currentMotherMenu == 2) // 第三个主菜单有子目录
        {
            menuContext.state = MENU_STATE_SUB;
            menuContext.currentSubMenu = 0; // 进入子菜单时从第一个开始
        }
    }
    displayMenu();
}

// 退出到上一级菜单
void exitMenu(void)
{
    if (menuContext.state == MENU_STATE_SUB)
    {
        menuContext.state = MENU_STATE_ROOT;
    }
    else if (menuContext.state == MENU_STATE_ROOT)
    {
        menuContext.state = MENU_STATE_MOTHER;
    }
    displayMenu();
}